home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / sys / os2 / pmserv / os2pm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-11  |  8.8 KB  |  365 lines

  1. /*
  2.     os2pm.c
  3.  
  4.     Geoffrey Furnish
  5.     9-22-91
  6.     
  7.     This driver sends plplot commands to the OS/2 PM PLPLOT Server.
  8.  
  9.     The Geoffrey Furnish Standard Disclaimer:
  10.     "I hate any C compiler that isn't ANSI compliant, and I refuse to waste
  11.     my time trying to support such garbage.  If you can't compile with an
  12.     ANSI compiler, then don't expect this to work.  No appologies,
  13.     now or ever."
  14.  
  15.     25 March 1992
  16.     VERSION 1.0
  17. */
  18.  
  19. /* Some screwy thing with VOID goes wrong if this comes after the
  20.    the os2.h stuff. */
  21.  
  22. #include "plplot.h"
  23.  
  24. #define INCL_BASE
  25. #include <os2.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "dispatch.h"
  29.  
  30. /* top level declarations */
  31.  
  32. static USHORT    rv;
  33. static HFILE    hf;
  34. static short    cnt;
  35.  
  36. static PLINT    xold = -100000;
  37. static PLINT    yold = -100000;
  38.       
  39. #include "pmdefs.h"
  40.  
  41. typedef    PLINT    COMMAND_ID;
  42. typedef PLINT * CPARAMS;
  43.  
  44. static void    write_command( COMMAND_ID cid, CPARAMS p );
  45.  
  46. /*----------------------------------------------------------------------*\
  47. *  os2setup()
  48. *
  49. * Set up device.
  50. \*----------------------------------------------------------------------*/
  51.  
  52. void os2setup    PLARGS(( PLStream *pls ))
  53. {
  54. }
  55.  
  56. /*----------------------------------------------------------------------*\
  57. *  os2orient()
  58. *
  59. * Set up orientation.
  60. \*----------------------------------------------------------------------*/
  61.  
  62. void os2orient    PLARGS(( PLStream *pls ))
  63. {
  64. }
  65.  
  66. /*----------------------------------------------------------------------*\
  67. *  os2init()
  68. *
  69. * Initialize device.
  70. \*----------------------------------------------------------------------*/
  71.  
  72. void    os2init    PLARGS(( PLStream *pls ))
  73. {
  74.     USHORT    usAction;
  75.     UCHAR    c = (UCHAR) INITIALIZE;
  76.  
  77.     pls->termin =- 0;        /* not an interactive terminal */
  78.     pls->color = 1;
  79.     pls->width = 1;
  80.     pls->bytecnt = 0;
  81.     pls->page = 1;
  82.  
  83.     setpxl( (PLFLT) PIXEL_RES_X, (PLFLT) PIXEL_RES_Y );
  84.     setphy( 0, PLMETA_X, 0, PLMETA_Y );
  85.  
  86.     rv = DosOpen( PIPE_NAME,        // name of the pipe.
  87.         &hf,            // address of file handle.
  88.         &usAction,        // action taken.
  89.         0L,            // size of file.
  90.         FILE_NORMAL,        // file attribute.
  91.         FILE_OPEN,        // open the file.
  92.         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
  93.         0L );
  94.     if (rv)
  95.     plexit( "Unable to open connection to PM server.\n" );
  96.     
  97.     write_command( c, NULL );
  98. }
  99.  
  100. /*----------------------------------------------------------------------*\
  101. *  os2line()
  102. *
  103. * Draw a line in the current color from (x1,y1) to (x2,y2).
  104. \*----------------------------------------------------------------------*/
  105.  
  106. void os2line    PLARGS(( PLStream *pls, 
  107.             PLINT x1, PLINT y1, PLINT x2, PLINT y2 ))
  108. {
  109.     UCHAR c;
  110.     PLINT    cp[4];
  111.  
  112.     if (    x1 < 0 || x1 > PLMETA_X ||
  113.         x2 < 0 || x2 > PLMETA_X ||
  114.         y1 < 0 || y1 > PLMETA_Y ||
  115.         y2 < 0 || y2 > PLMETA_Y     ) {
  116.         printf( "Something is out of bounds." );
  117.     }
  118.  
  119. /* If continuation of previous line send the LINETO command, which uses 
  120.    the previous (x,y) point as it's starting location.  This results in a
  121.    storage reduction of not quite 50%, since the instruction length for
  122.    a LINETO is 5/9 of that for the LINE command, and given that most 
  123.    graphics applications use this command heavily.
  124.  
  125.    Still not quite as efficient as tektronix format since we also send the
  126.    command each time (so shortest command is 25% larger), but a heck of
  127.    a lot easier to implement than the tek method.
  128. */
  129.     if(x1 == xold && y1 == yold) {
  130.  
  131.         c = (UCHAR) LINETO;
  132.         cp[0]=x2;
  133.         cp[1]=y2;
  134.         write_command( c, cp );
  135.     }
  136.     else {
  137.         c = (UCHAR) LINE;
  138.         cp[0] = x1;
  139.         cp[1] = y1;
  140.         cp[2] = x2;
  141.         cp[3] = y2;
  142.         write_command( c, cp );
  143.     }
  144.     xold = x2;
  145.     yold = y2;
  146. }
  147.   
  148. /*----------------------------------------------------------------------*\
  149. *  os2clear()
  150. *
  151. *  Clear page.
  152. \*----------------------------------------------------------------------*/
  153.  
  154. void    os2clear    PLARGS(( PLStream *pls ))
  155. {
  156.     UCHAR c = (UCHAR) CLEAR;
  157.  
  158.     write_command( c, NULL );
  159. }
  160.  
  161. /*----------------------------------------------------------------------*\
  162. *  os2page()
  163. *
  164. *  Advance to next page.
  165. \*----------------------------------------------------------------------*/
  166.  
  167. void    os2page    PLARGS(( PLStream *pls ))
  168. {
  169.     UCHAR c = (UCHAR) PAGE;
  170.  
  171.     xold = -100000;
  172.     yold = -100000;
  173.  
  174.     write_command( c, NULL );
  175. }
  176.  
  177. /*----------------------------------------------------------------------*\
  178. * os2adv()
  179. *
  180. * Advance to the next page.
  181. * Also write page information as in plmpage().
  182. \*----------------------------------------------------------------------*/
  183.  
  184. void    os2adv    PLARGS(( PLStream *pls ))
  185. {
  186.     os2clear(pls);
  187.     os2page(pls);
  188. }
  189.  
  190. /*----------------------------------------------------------------------*\
  191. *  os2tidy()
  192. *
  193. *  Close graphics file
  194. \*----------------------------------------------------------------------*/
  195.  
  196. void    os2tidy    PLARGS(( PLStream *pls ))
  197. {
  198.     UCHAR c = (UCHAR) CLOSE;
  199.     
  200.     write_command( c, NULL );
  201.          
  202.     DosClose( hf );
  203.     pls->fileset = 0;
  204. }
  205.  
  206. /*----------------------------------------------------------------------*\
  207. *  os2color()
  208. *
  209. *  Set pen color.
  210. \*----------------------------------------------------------------------*/
  211.  
  212. void    os2color    PLARGS(( PLStream *pls ))
  213. {
  214.     UCHAR c = (UCHAR) NEW_COLOR;
  215.     
  216.     write_command( c, &pls->color );
  217. }
  218.  
  219. /*----------------------------------------------------------------------*\
  220. *  os2text()
  221. *
  222. *  Switch to text mode.
  223. \*----------------------------------------------------------------------*/
  224.  
  225. void    os2text    PLARGS(( PLStream *pls ))
  226. {
  227.     UCHAR c = (UCHAR) SWITCH_TO_TEXT;
  228.  
  229.     write_command( c, NULL );
  230. }
  231.  
  232. /*----------------------------------------------------------------------*\
  233. *  os2graph()
  234. *
  235. *  Switch to graphics mode.
  236. \*----------------------------------------------------------------------*/
  237.  
  238. void    os2graph    PLARGS(( PLStream *pls ))
  239. {
  240.     UCHAR c = (UCHAR) SWITCH_TO_GRAPH;
  241.  
  242.     write_command( c, NULL );
  243. }
  244.  
  245. /*----------------------------------------------------------------------*\
  246. *  os2width()
  247. *
  248. *  Set pen width.
  249. \*----------------------------------------------------------------------*/
  250.  
  251. void    os2width    PLARGS(( PLStream *pls ))
  252. {
  253.     UCHAR c = (UCHAR) NEW_WIDTH;
  254.     
  255.     write_command( c, &pls->width );
  256. }
  257.  
  258. /*----------------------------------------------------------------------*\
  259. *  os2esc()
  260. *
  261. *  Escape function.  Note that any data written must be in device
  262. *  independent form to maintain the transportability of the metafile.
  263. \*----------------------------------------------------------------------*/
  264.  
  265. void    os2esc    PLARGS(( PLStream *pls, PLINT op, char *ptr ))
  266. {
  267.     UCHAR c = (UCHAR) ESCAPE;
  268.     float *color;
  269.     unsigned long ired, igreen, iblue;
  270.     unsigned long    pmrgb;
  271.  
  272.     write_command( c, NULL );
  273.  
  274.     switch (op) {
  275.         case PL_SET_RGB: 
  276.         c = (UCHAR) ESC_RGB;
  277.         color = (float *) &ptr[0];
  278.         ired =    min(256,max(0,(int)255.*color[0]));
  279.         igreen= min(256,max(0,(int)255.*color[1]));
  280.         iblue = min(256,max(0,(int)255.*color[2]));
  281.         pmrgb    = (ired   & 0xff) << 16 |
  282.               (igreen & 0xff) <<  8 |
  283.               (iblue  & 0xff);
  284.         write_command( c, &pmrgb );
  285.         //printf( "Changing to RGB value %lx \n", pmrgb );
  286.         break;
  287.     
  288.         default:
  289.         c = (UCHAR) ESC_NOOP;
  290.         write_command( c, NULL );
  291.     }
  292. }
  293.  
  294. /*----------------------------------------------------------------------*\
  295. * Support routines
  296. \*----------------------------------------------------------------------*/
  297.  
  298. /*----------------------------------------------------------------------*\
  299. *    write_command()
  300. *    
  301. *    Function to write a command to the command pipe, and to flush
  302. *    the pipe when full.
  303. \*----------------------------------------------------------------------*/
  304.  
  305. void    write_command( COMMAND_ID cid,    CPARAMS p )
  306. {
  307.     static int i=0;
  308.     static PLINT buffer[ PIPE_BUFFER_SIZE ];
  309.     static PLINT cnt = 0;
  310.     
  311.     i++;
  312.  
  313.     buffer[cnt++] = cid;
  314.     switch( cid ) {
  315.     case LINE:
  316.         buffer[cnt++] = *p++;
  317.         buffer[cnt++] = *p++;
  318.         buffer[cnt++] = *p++;
  319.         buffer[cnt++] = *p++;
  320.         break;
  321.  
  322.     case LINETO:
  323.         buffer[cnt++] = *p++;
  324.         buffer[cnt++] = *p++;
  325.         break;
  326.  
  327.     case NEW_COLOR:
  328.     case NEW_WIDTH:
  329.     case ESC_RGB:
  330.         buffer[cnt++] = *p++;
  331.         break;
  332.  
  333.     case INITIALIZE:
  334.     case CLOSE:
  335.     case SWITCH_TO_TEXT:
  336.     case SWITCH_TO_GRAPH:
  337.     case CLEAR:
  338.     case PAGE:
  339.     case ESC_NOOP:
  340.         break;
  341.     
  342.     case ESCAPE:
  343.         break;
  344.  
  345.     default:
  346.         printf( "Unknown command type--no params passed\n" );
  347.         break;
  348.     }
  349.     if (cnt >= (.9 * PIPE_BUFFER_SIZE) || cid == CLOSE) {
  350.     short rv1, rv2, bytes1, bytes2;
  351.     
  352.     rv1 = DosWrite( hf, &cnt, sizeof( PLINT ), &bytes1 );
  353.     rv2 = DosWrite( hf, buffer, (USHORT) (cnt * sizeof(PLINT)), &bytes2 );
  354.     if (!rv1 && !rv2) 
  355.         /* printf( "%d, %d bytes were written.\n", bytes1, bytes2 ) */ ;
  356.     else 
  357.         printf( "----> write to pipe failed <----\n" );
  358.  
  359.     if (bytes1 != 4 || bytes2 != cnt*sizeof(PLINT) )
  360.         printf( "*** Bogus # of bytes written ***\n" );
  361.  
  362.     cnt=0;
  363.     }
  364. }
  365.